home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_UDELxntp.idb / usr / freeware / src / xntp-3.4o-export / lib / authdecrypt.c.z / authdecrypt.c
C/C++ Source or Header  |  1998-01-21  |  2KB  |  83 lines

  1. /*
  2.  * authdecrypt - routine to decrypt a packet to see if this guy knows our key.
  3.  */
  4. #include "ntp_stdlib.h"
  5.  
  6. /*
  7.  * For our purposes an NTP packet looks like:
  8.  *
  9.  *    a variable amount of unencrypted data, multiple of 8 bytes, followed by:
  10.  *    NOCRYPT_OCTETS worth of unencrypted data, followed by:
  11.  *    BLOCK_OCTETS worth of ciphered checksum.
  12.  */ 
  13. #define    NOCRYPT_OCTETS    4
  14. #define    BLOCK_OCTETS    8
  15.  
  16. #define    NOCRYPT_int32S    ((NOCRYPT_OCTETS)/sizeof(u_int32))
  17. #define    BLOCK_int32S    ((BLOCK_OCTETS)/sizeof(u_int32))
  18.  
  19. /*
  20.  * Imported from the key data base module
  21.  */
  22. extern u_long cache_keyid;    /* cached key ID */
  23. extern u_char DEScache_dkeys[];    /* cached decryption keys */
  24. extern u_char DESzerodkeys[];    /* zero key decryption keys */
  25.  
  26. /*
  27.  * Stat counters, imported from data base module
  28.  */
  29. extern u_int32 authdecryptions;
  30. extern u_int32 authkeyuncached;
  31.  
  32. int
  33. DESauthdecrypt(keyno, pkt, length)
  34.     u_long keyno;
  35.     const u_int32 *pkt;
  36.     int length;    /* length of variable data in octets */
  37. {
  38.     register const u_int32 *pd;
  39.     register int i;
  40.     register u_char *keys;
  41.     register int longlen;
  42.     u_int32 work[2];
  43.  
  44.     authdecryptions++;
  45.     
  46.     if (keyno == 0)
  47.         keys = DESzerodkeys;
  48.     else {
  49.         if (keyno != cache_keyid) {
  50.             authkeyuncached++;
  51.             if (!authhavekey(keyno))
  52.                 return 0;
  53.         }
  54.         keys = DEScache_dkeys;
  55.     }
  56.  
  57.     /*
  58.      * Get encryption block data in host byte order and decrypt it.
  59.      */
  60.     longlen = length / sizeof(u_int32);
  61.     pd = pkt + longlen;        /* points at NOCRYPT area */
  62.     work[0] = *(pd + NOCRYPT_int32S);
  63.     work[1] = *(pd + NOCRYPT_int32S + 1);
  64.  
  65.     if (longlen & 0x1) {
  66.         DESauth_des(work, keys);
  67.         work[0] ^= *(--pd);
  68.     }
  69.  
  70.     for (i = longlen/2; i > 0; i--) {
  71.         DESauth_des(work, keys);
  72.         work[1] ^= *(--pd);
  73.         work[0] ^= *(--pd);
  74.     }
  75.  
  76.     /*
  77.      * Success if the encryption data is zero
  78.      */
  79.     if ((work[0] == 0) && (work[1] == 0))
  80.         return 1;
  81.     return 0;
  82. }
  83.